<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Zwop_system extends CI_Controller{
    function __construct()
    {
      parent::__construct();

      if( !$this->session->userdata('isLoggedIn') ) {
          redirect('/login/show_login');
      }
    }

    public function system_manager() {
                
        $systemFlag=array("iis"=>"IIS重启","system"=>"业务系统重启","regsvr"=>"Excel驱动注册","zwdp"=>"小额服务重启","sk"=>"收款服务重启","pay"=>"付款服务重启","frontpay"=>"前置直付系统重启","marsapi"=>"客服API重启");
        $data['sidebar_type'] = "system";
        $data['sidebar_list'] = $systemFlag;
        $data['is_admin'] = $this->session->userdata('isadmin'); 
        $this->load->view('zwop_system',$data);
    }
    public function get_restart_list($restart_flag) {
        $this->load->model('system_m');
        if( $restart_flag == "iis" OR $restart_flag == "regsvr" ) {
            $result=$this->system_m->get_system_all();
            if ($result) {
                foreach( $result as $row ) {
                    $objList[] = $row['system_ip'];
                }
            }
        }elseif( $restart_flag == "system" ){
            $result=$this->system_m->get_system_all();
            if($result) {
                foreach( $result as $row ) {
                    $objList[] = $row['system_name'];
                }
            }
        }else {
            $system_name="";
            switch($restart_flag){
            case "zwdp":
                $system_name="ZWDirectpayDP";
                break;
            case "sk":
                $system_name="UploadBankDataService";
                break;
            case "pay":
                $system_name="FinanceTaskManageService";
                break;
            case "frontpay":
                $system_name="FrontDirectPay";
                break;
            case "marsapi":
                $system_name="MarsAPI";
                break;
            default:
                $system_name="";
                break;
            }
            $result=$this->system_m->get_system_detail($system_name);
            if($result){
                $objList[] = $result['system_ip'];
                $ipOther = explode(";",$result['system_ip_other']);
                foreach($ipOther as $ip){
                    $objList[] = $ip;
                }
            }
        }
        if ( is_array($objList) && count($objList) > 0 ){
            return $objList;
        }
        return false;
    }
    public function system_restart($restart_flag=null) {
        $systemFlag=array("iis"=>"IIS重启","system"=>"业务系统重启","regsvr"=>"Excel驱动注册","zwdp"=>"小额服务重启","sk"=>"收款服务重启","pay"=>"付款服务重启","frontpay"=>"前置直付系统重启","marsapi"=>"客服API重启");
        $restart_list=$this->get_restart_list($restart_flag);
        $data['sidebar_type'] = "system";
        $data['sidebar_list'] = $systemFlag;
        $data['restart_list'] = $restart_list;
        $data['restart_flag'] = $restart_flag;
        $data['is_admin'] = $this->session->userdata('isadmin');
        $this->load->view('zwop_system',$data);
    }
    public function exec_restart() {
        $cmd="";
        $errorStr="";
        $system_pwd="";
        $system_ip="";
        $this->load->model('system_m');
        $restart_object = $this->input->post('restart_object');
        $restart_flag = $this->input->post('restart_flag');
        if ($restart_flag=="system") {
            $result = $this->system_m->get_system_detail($restart_object);
            
            if($result){
                $system_ip=$result['system_ip'];
                $resultPwd=$this->system_m->get_ip_passwd($system_ip);
                if($resultPwd){
                    $system_pwd = $resultPwd['password'];
                    $resultDevice=$this->system_m->get_device_detail($system_ip);
                    
                    if($resultDevice){
                        if($resultDevice['device_bit'] == 32){
                            $cmd = sprintf("/cygdrive/c/WINDOWS/system32/cscript d:/shell/op.vbs %s restart",$restart_object); 
                        }else if($resultDevice['device_bit'] == 64){
                            $cmd = sprintf("/cygdrive/c/WINDOWS/system32/cscript d:/shell/op_64.vbs %s restart",$restart_object);
                        }
                    }else{
                        $errorStr=sprintf("%s os bit not confirm,please check",$system_ip);
                    }
                }else{
                    $errorStr=sprintf("%s password not exist,please check",$system_ip);
                }
            }else{
                $errorStr=sprintf("%s not exist,please check",$restart_object);
            }
        }
        else{
            $result = $this->system_m->get_ip_passwd($restart_object);
            if($result){
                $system_pwd = $result['password'];
                $system_ip = $restart_object;
                
                switch($restart_flag){
                case "iis":
                    $cmd = "/cygdrive/c/WINDOWS/system32/iisreset /restart";
                    break;
                case "regsvr":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/op_excel.vbs";
                    break;
                case "zwdp":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/restart_service.vbs ZWDirectPayDP.HostService";
                    break;
                case "sk":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/restart_service.vbs UploadBankDataService";
                    break;
                case "pay":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/restart_service.vbs FinanceTaskManageService";
                    break;
                case "frontpay":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/op_64.vbs FrontDirectPay restart";
                    break;
                case "marsapi":
                    $cmd = "/cygdrive/c/WINDOWS/system32/cscript d:/shell/op_64.vbs MarsAPI restart";
                    break;
                default:break;
                }
            }else{
                $errorStr = sprintf("%s password not ok,please check",$restart_object);
            }
        }
         
        if ( isset($errorStr) && empty($errorStr) ){
            $params=array('host'=>$system_ip,'port'=>'36000','passwd'=>$system_pwd,'user'=>'Administrator','cmd'=>$cmd);
           // $this->load->library('ssh',$params);
           // $this->ssh->authPassword('Administrator',$system_pwd);
           // $this->ssh->cmdExec();
            //  $response = $this->ssh->getLog();
          //  $cmd="ls /cygdrive/d";
            $exec_cmd=sprintf("python %s/shell/py_ssh_rsync.py %s Administrator %s '%s'",APPPATH,$system_ip,$system_pwd,$cmd);
            $response=shell_exec($exec_cmd);
            echo $response;
        }else{
            echo $errorStr;
        }
    } 
}
